/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 25397
 * Date: 2021-12-03
 * Time: 16:37
 */
//package com.shiyan7;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class MyClock extends JFrame  {
    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        // ����Ĭ����۸о�
        JFrame.setDefaultLookAndFeelDecorated(true);
        JDialog.setDefaultLookAndFeelDecorated(true);
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MyClock frame = new MyClock();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private Thread timeThread2 = new Thread() {//���������ڲ��������
        public void run(){
            while(true)
            {
                try {
                    String time = sdf.format(new Date());
                    clockLabel.setText(time);
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    };

    private Thread timeThread = new Thread(
            new Runnable() {//��������ʵ�������
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    while(true)
                    {
                        try {
                            String time = sdf.format(new Date());
                            clockLabel.setText(time);
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            });

    /**
     * Create the frame.
     */
    public MyClock() {
        setTitle("\u6211\u7684\u65F6\u949F");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 321, 131);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        clockLabel = new JLabel("New label");
        contentPane.add(clockLabel, BorderLayout.CENTER);
        clockLabel.setText("");

        JButton btnNewButton = new JButton("\u663E\u793A\u65F6\u95F4");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                //����д�����ť�Ժ�����ִ�еĴ���
                if(!timeThread.isAlive()) {
                    System.out.println("����ʱ���߳�");
                    timeThread.start();
                }
            }
        });
        contentPane.add(btnNewButton, BorderLayout.NORTH);

    }
    JLabel clockLabel = null;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
